local function onmax(self, max)
    self.inst.replica.ta_fragmentate:SetMax(max)
end

local function oncurrent(self, current)
    self.inst.replica.ta_fragmentate:SetCurrent(current)
end

local TaFragmentate = Class(function(self, inst)
    self.inst = inst
    self.max = TUNING.TWILIGHT_ARCHANGEL_FRAGMENTATE -- 最大值
    self.current = self.max -- 当前值
    self.death = false
end, nil, {
    max = onmax,
    current = oncurrent
})

function TaFragmentate:OnSave() -- 保存
    return {
        current = self.current,
        death = self.death
    } -- 保存当前值
end

function TaFragmentate:OnLoad(data) -- 加载
    if data.current ~= nil then
        self.current = data.current
        self:DoDelta(0)
    end
    if data.death ~= nil then
        self.death = data.death
    end
    self.inst:DoTaskInTime(3, function()
        self:Apply()
    end)

end

function TaFragmentate:SetMax(amount) -- 设置最大值
    self.max = amount
    self.current = amount
end

function TaFragmentate:DoDelta(delta) -- 改变的函数

    local old = self.current
    self.current = math.clamp(self.current + delta, 0, self.max)

    -- 其实改变的时候事件和需要传的参数都是随自己看需求写的
    self.inst:PushEvent("ta_fragmentate_delta", {
        oldpercent = old / self.max,
        newpercent = self.current / self.max
    })
    self:Apply()

    -- 破碎值耗尽后死亡
    if self.current <= 0 then
        -- self.inst:PushEvent("death","破碎值耗尽") 
        self.death = true
        self.inst.components.health:DoDelta(-10000000, false, "ta_fragmentate_over")
    end

end
function TaFragmentate:Apply()

    if self.current <= TUNING.TWILIGHT_ARCHANGEL_FRAGMENTATE * 0.25 then
        -- 破碎值低于25时3倍伤害，受到0.4倍伤害
        -- 破碎值低于25时造成伤害改为造成5倍受到0.25倍伤害
        self.inst.components.combat.damagemultiplier = 5
        self.inst.components.health.absorb = 0.75 -- 减少0.6
    elseif self.current <= TUNING.TWILIGHT_ARCHANGEL_FRAGMENTATE * 0.75 then
        -- 破碎值低于75点时攻击为1.75倍伤害，受到0.75倍伤害
        self.inst.components.combat.damagemultiplier = 1.75
        self.inst.components.health.absorb = 0.25 -- 减少0.25 
    else
        -- 伤害系数
        self.inst.components.combat.damagemultiplier = 1.25
        -- 伤害减免。
        self.inst.components.health.absorb = -0.25
    end

    if self.current <= TUNING.TWILIGHT_ARCHANGEL_FRAGMENTATE * 0.75 then
        self.inst.components.locomotor:SetExternalSpeedMultiplier(self.inst, "twilight_archangel_speed_mod", 1.25)
    end
    -- if self.inst.components.worker == nil then
    --     self.inst:AddComponent("worker")
    -- end
    -- if self.current <= TUNING.TWILIGHT_ARCHANGEL_FRAGMENTATE * 0.25 then
    --     self.inst.components.worker:SetAction(ACTIONS.CHOP, 1000)
    --     self.inst.components.worker:SetAction(ACTIONS.MINE, 1000)
    --     self.inst.components.worker:SetAction(ACTIONS.DIG, 1000)
    --     self.inst.components.worker:SetAction(ACTIONS.HAMMER, 1000)
    --     self.inst:AddTag("no_ta_25")
    -- else
    --     self.inst.components.worker:SetAction(ACTIONS.CHOP, 1)
    --     self.inst.components.worker:SetAction(ACTIONS.MINE, 1)
    --     self.inst.components.worker:SetAction(ACTIONS.DIG, 1)
    --     self.inst.components.worker:SetAction(ACTIONS.HAMMER, 1)
    -- end

    self:ApplyHealthPenalty()
end
function TaFragmentate:ApplyHealthPenalty()
    print("self.current / self.max", 1 - self.current / self.max)
    -- self.inst.components.health:SetPenalty(1-self.current / self.max)
    if not self.inst.components.health.disable_penalty then
        -- Penalty should never be less than 0% or ever above 75%.
        self.inst.components.health.penalty = math.clamp(1 - self.current / self.max, 0, 0.99)
    end
    self.inst.components.health:ForceUpdateHUD(true) -- handles capping health at max with penalty
end

function TaFragmentate:GetPercent() -- 获取百分比
    return self.current / self.max
end

function TaFragmentate:SetPercent(p) -- 设置百分比
    local old = self.current
    self.current = p * self.max
    self.inst:PushEvent("ta_fragmentate_delta", {
        oldpercent = old / self.max,
        newpercent = p
    })
end

--[[
    如果需要别的变量 和方法 看自己需求自己加吧

    ThePlayer.components.ta_fragmentate:SetPercent(0.8)

]]

return TaFragmentate
